home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr35 / jamapi.zip / HMB2JAM.ZIP / HMB2JAM.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  11KB  |  451 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  HMB to JAM converter
  5. **
  6. **  Written by Mats Wallin
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  hmb2jam.c (JAMmb)
  11. **
  12. **  Program that moves messages from a HMB message base to a
  13. **  JAM message base
  14. **
  15. **  Requires that the HMB messagebase is located in the current directory
  16. **
  17. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  18. **  Mats Wallin. ALL RIGHTS RESERVED.
  19. **
  20. **  93-06-28    MW
  21. **  Initial coding.
  22. */
  23.  
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <io.h>
  27. #include <share.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32.  
  33. #include "jammb.h"
  34.  
  35. #include "hmbsub.h"
  36. #include "jamsub.h"
  37.  
  38.  
  39. /*
  40. **  The largest supported size of a HMB message text, in records (256 bytes)
  41. */
  42.  
  43. #define MAXHMBRECS    200
  44.  
  45.  
  46. /*
  47. **  Global variables
  48. */
  49.  
  50. JAMAPIREC       JamRec;
  51. HMBREC          HmbRec;
  52.  
  53. AKA             DestAddr,
  54.                 OrigAddr;
  55.  
  56. UCHAR8          MsgTxt [MAXHMBRECS * sizeof( HMBTXT )];
  57.  
  58. UINT32          SubFldPos;
  59.  
  60.  
  61. /*
  62. **  Prototypes
  63. */
  64.  
  65. int ScanMsgs( CHAR8 * pJamFileName, int Board );
  66. int MoveThisMsg( HMBREC * pHmb, JAMAPIREC * pJam );
  67. int HandleFtscKludge( JAMAPIREC * pJam, CHAR8 * pStr );
  68. CHAR8 * GetKludgeContents( CHAR8 * pStr );
  69.  
  70.  
  71. /* ---------------------------------------------------------------------- *
  72.  *
  73.  *  main
  74.  *
  75.  * ---------------------------------------------------------------------- */
  76.  
  77. int main( int argc, CHAR8 * argv [] )
  78. {
  79.   int   Board;
  80.   CHAR8 JamFileName [128];
  81.  
  82.   puts( "HMB to JAM converter\n"
  83.         "Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and\n"
  84.         "               Mats Wallin. ALL RIGHTS RESERVED.\n"
  85.         "Written by Mats Wallin\n" );
  86.  
  87.   if( argc != 3 )
  88.     {
  89.     puts( "Format: HMB2JAM <board> <JAMfile>\n"
  90.           "where: <board>   is the HMB board number\n"
  91.           "       <JAMfile> is the path and base filename to the JAM messagebase\n" );
  92.     return( 1 );
  93.     }
  94.  
  95.   if(( Board = atoi( argv [1] )) < 1 || Board > 200 )
  96.     {
  97.     printf( "Invalid HMB board number specified: %s\n", argv [1] );
  98.     return( 1 );
  99.     }
  100.  
  101.   strcpy( JamFileName, argv [2] );
  102.  
  103.  
  104. /*
  105. **  Open the HMB messagebase
  106. */
  107.  
  108.   if( !InitHMBREC( &HmbRec ))
  109.     return( 0 );
  110.  
  111.   if( !HmbOpen( &HmbRec, JAMO_RDONLY, JAMSH_DENYNO ))
  112.     return( 0 );
  113.  
  114.  
  115.   if( !ScanMsgs( JamFileName, Board ))
  116.     return( 1 );
  117.  
  118.  
  119. /*
  120. **  Close the HMB messagebase
  121. */
  122.  
  123.   HmbClose( &HmbRec );
  124.   return( 0 );
  125. }
  126.  
  127.  
  128. /* ---------------------------------------------------------------------- *
  129.  *
  130.  *  ScanMsgs
  131.  *
  132.  * ---------------------------------------------------------------------- */
  133.  
  134. int ScanMsgs( CHAR8 * pJamFileName, int Board )
  135. {
  136.   int   done;
  137.  
  138.   if( !JAMsysInitApiRec( &JamRec, pJamFileName, WORKBUFSIZE ))
  139.     {
  140.     puts( "Not enough memory" );
  141.     return( 0 );
  142.     }
  143.  
  144.  
  145. /*
  146. **  Open the JAM messagebase
  147. */
  148.  
  149.   if( !JAMmbOpen( &JamRec ))
  150.     {
  151.     if( JamRec.Errno != ENOENT )
  152.       {
  153.       perror( "Unable to open the JAM messagebase" );
  154.       JAMsysDeinitApiRec( &JamRec );
  155.       return( 0 );
  156.       }
  157.  
  158.     if( !JAMmbCreate( &JamRec ))
  159.       {
  160.       perror( "Unable to create the JAM messagebase" );
  161.       JAMsysDeinitApiRec( &JamRec );
  162.       return( 0 );
  163.       }
  164.     }
  165.  
  166.  
  167. /*
  168. **  Read all messages in the HMB messagebase, and search for those in the
  169. **  correct board
  170. */
  171.  
  172.   done = HmbReadFirstHdr( &HmbRec );
  173.   while( !done )
  174.     {
  175.     if( HmbRec.Hdr.Board == ( UCHAR8 ) Board )
  176.       {
  177.       printf( "Message %u\n", HmbRec.Hdr.MsgNo );
  178.       MoveThisMsg( &HmbRec, &JamRec );
  179.       }
  180.  
  181.     done = HmbReadNextHdr( &HmbRec );
  182.     }
  183.  
  184.  
  185. /*
  186. **  Close the JAM messagebase
  187. */
  188.  
  189.   if( !JAMmbClose( &JamRec ))
  190.     {
  191.     perror( "Unable to close the JAM messagebase" );
  192.     return( 0 );
  193.     }
  194.  
  195.   JAMsysDeinitApiRec( &JamRec );
  196.  
  197.   return( 1 );
  198. }
  199.  
  200.  
  201. /* ---------------------------------------------------------------------- *
  202.  *
  203.  *  MoveThisMsg
  204.  *
  205.  * ---------------------------------------------------------------------- */
  206.  
  207. int MoveThisMsg( HMBREC * pHmb, JAMAPIREC * pJam )
  208. {
  209.   CHAR8         String [256],
  210.                 Str2 [128];
  211.   UCHAR8      * pSrc,
  212.               * pDst;
  213.   int           NumRecs,
  214.                 i;
  215.   unsigned int  MsgTxtSize;
  216.  
  217.  
  218. /*
  219. **  If the message is deleted, skip it
  220. */
  221.  
  222.   if( pHmb->Hdr.MsgAttr & HMB_DELETED )
  223.     return( 0 );
  224.  
  225.  
  226. /*
  227. **  First process the header
  228. */
  229.  
  230.   JamMsgInit( pJam );
  231.   SubFldPos = 0;
  232.  
  233.   if( pHmb->Hdr.MsgAttr & HMB_NETMAIL )
  234.     {
  235.     OrigAddr.Zone  = pHmb->Hdr.OrigZone;
  236.     OrigAddr.Net   = pHmb->Hdr.OrigNet;
  237.     OrigAddr.Node  = pHmb->Hdr.OrigNode;
  238.     OrigAddr.Point = 0;
  239.  
  240.     DestAddr.Zone  = pHmb->Hdr.DestZone;
  241.     DestAddr.Net   = pHmb->Hdr.DestNet;
  242.     DestAddr.Node  = pHmb->Hdr.DestNode;
  243.     DestAddr.Point = 0;
  244.     }
  245.  
  246.   Pascal2C( pHmb->Hdr.WhoFrom, String );
  247.   JamMsgAddSFldStr( pJam, JAMSFLD_SENDERNAME, String, &SubFldPos );
  248.  
  249.   Pascal2C( pHmb->Hdr.WhoTo, String );
  250.   JamMsgAddSFldStr( pJam, JAMSFLD_RECVRNAME, String, &SubFldPos );
  251.  
  252.   Pascal2C( pHmb->Hdr.Subject, String );
  253.   if( pHmb->Hdr.NetAttr & HMB_FILE )
  254.     JamMsgAddSFldStr( pJam, JAMSFLD_ENCLFILE, String, &SubFldPos );
  255.   else
  256.     JamMsgAddSFldStr( pJam, JAMSFLD_SUBJECT, String, &SubFldPos );
  257.  
  258.   pJam->Hdr.TimesRead = pHmb->Hdr.TimesRead;
  259.   pJam->Hdr.Cost      = pHmb->Hdr.Cost;
  260.  
  261.   Pascal2C( pHmb->Hdr.PostDate, String );
  262.   Pascal2C( pHmb->Hdr.PostTime, Str2 );
  263.   pJam->Hdr.DateWritten = HmbDateTimeToUnix( String, Str2 );
  264.   pJam->Hdr.DateProcessed = JAMsysTime( NULL );
  265.  
  266.   if( pHmb->Hdr.MsgAttr & HMB_NETMAIL )
  267.     pJam->Hdr.Attribute |= MSG_TYPENET;
  268.   else
  269.     pJam->Hdr.Attribute |= MSG_TYPEECHO;
  270.  
  271.   if( pHmb->Hdr.MsgAttr & HMB_PVT     ) pJam->Hdr.Attribute |= MSG_PRIVATE;
  272.   if( pHmb->Hdr.MsgAttr & HMB_RCVD    ) pJam->Hdr.Attribute |= MSG_READ;
  273.   if( pHmb->Hdr.MsgAttr & HMB_LOCAL   ) pJam->Hdr.Attribute |= MSG_LOCAL;
  274.  
  275.   if( pHmb->Hdr.NetAttr & HMB_KILL    ) pJam->Hdr.Attribute |= MSG_KILLSENT;
  276.   if( pHmb->Hdr.NetAttr & HMB_SENT    ) pJam->Hdr.Attribute |= MSG_SENT;
  277.   if( pHmb->Hdr.NetAttr & HMB_FILE    ) pJam->Hdr.Attribute |= MSG_FILEATTACH;
  278.   if( pHmb->Hdr.NetAttr & HMB_CRASH   ) pJam->Hdr.Attribute |= MSG_CRASH;
  279.   if( pHmb->Hdr.NetAttr & HMB_RR_REQ  ) pJam->Hdr.Attribute |= MSG_RECEIPTREQ;
  280.  
  281.  
  282. /*
  283. **  And now it's time for the actual messagetext
  284. */
  285.  
  286.   JAMsysSeek( NULL, pHmb->TxtHandle, JAMSEEK_SET, ( INT32 ) pHmb->Hdr.StartRec * ( INT32 ) sizeof( HMBTXT ) );
  287.  
  288.   NumRecs = min( pHmb->Hdr.NumRecs, MAXHMBRECS );
  289.   JAMsysRead( NULL, pHmb->TxtHandle, MsgTxt, NumRecs * sizeof( HMBTXT ));
  290.  
  291.  
  292. /*
  293. **  Copy the messagetext in the buffer, so we can get rid of the Length byte
  294. **  in each record
  295. */
  296.  
  297.   pSrc        = MsgTxt + 1;
  298.   pDst        = MsgTxt;
  299.   MsgTxtSize  = 0;
  300.  
  301.   for( i = 0; i < NumRecs; i++, pSrc += sizeof( HMBTXT ))
  302.     {
  303.     int   RecSize = pSrc [-1];
  304.  
  305.     memmove( pDst, pSrc, RecSize );
  306.  
  307.     pDst        += RecSize;
  308.     MsgTxtSize  += RecSize;
  309.     }
  310.  
  311.   *pDst = '\0';
  312.   MsgTxtSize++;
  313.  
  314.  
  315. /*
  316. **  Locate all FTSC kludge lines
  317. **  They should be stored as SubFields in the header
  318. */
  319.  
  320.   pDst = MsgTxt;
  321.   do
  322.     {
  323.     while( *pDst == '\r' || *pDst == '\n' || *pDst == 0x8D )
  324.       pDst++;
  325.  
  326.     while( *pDst == '\x01' )
  327.       {
  328.       if(( pSrc = ( UCHAR8 * ) strchr(( CHAR8 * ) pDst, '\r' )) != NULL )
  329.         *pSrc = '\0';
  330.  
  331.       strcpy( String, ( CHAR8 * ) pDst + 1 );
  332.       HandleFtscKludge( pJam, String );
  333.  
  334.       if( pSrc )
  335.         {
  336.         pSrc++;
  337.         while( *pSrc == '\n' || *pDst == 0x8D )
  338.           pSrc++;
  339.  
  340.         MsgTxtSize -= ( unsigned int ) ( pSrc - pDst );
  341.         memmove( pDst, pSrc, ( int ) ( MsgTxt + MsgTxtSize - pDst ));
  342.         }
  343.       }
  344.     }
  345.   while(( pDst = ( UCHAR8 * ) strchr(( CHAR8 * ) pDst, '\r' )) != NULL );
  346.  
  347.  
  348. /*
  349. **  If this is a netmail, add the origin and destination address
  350. */
  351.  
  352.   if( pHmb->Hdr.MsgAttr & HMB_NETMAIL )
  353.     {
  354.     sprintf( String, OrigAddr.Point != 0 ? "%u:%u/%u.%u" : "%u:%u/%u",
  355.                 OrigAddr.Zone, OrigAddr.Net, OrigAddr.Node, OrigAddr.Point );
  356.     JamMsgAddSFldStr( pJam, JAMSFLD_OADDRESS, String, &SubFldPos );
  357.  
  358.     sprintf( String, DestAddr.Point != 0 ? "%u:%u/%u.%u" : "%u:%u/%u",
  359.                 DestAddr.Zone, DestAddr.Net, DestAddr.Node, DestAddr.Point );
  360.     JamMsgAddSFldStr( pJam, JAMSFLD_DADDRESS, String, &SubFldPos );
  361.     }
  362.  
  363.  
  364. /*
  365. **  Now, start to write this message
  366. */
  367.  
  368.   pJam->Hdr.SubfieldLen = SubFldPos;
  369.   pJam->Hdr.TxtLen = MsgTxtSize;
  370.  
  371.   JamMsgWrite( pJam, ( CHAR8 * ) MsgTxt );
  372.   JamMsgDeinit( pJam );
  373.  
  374.   return( 1 );
  375. }
  376.  
  377.  
  378. /* ---------------------------------------------------------------------- *
  379.  *
  380.  *  HandleFtscKludge
  381.  *
  382.  * ---------------------------------------------------------------------- */
  383.  
  384. int HandleFtscKludge( JAMAPIREC * pJam, CHAR8 * pStr )
  385. {
  386.   CHAR8      Kludge [32];
  387.  
  388.   strncpy( Kludge, pStr, sizeof( Kludge ));
  389.   Kludge [sizeof( Kludge ) - 1] = '\0';
  390.   strtok( Kludge, ": " );
  391.  
  392.   if( !strcmp( Kludge, "MSGID" ))
  393.     JamMsgAddSFldStr( pJam, JAMSFLD_MSGID, GetKludgeContents( pStr ), &SubFldPos );
  394.  
  395.   else if( !strcmp( Kludge, "REPLY" ))
  396.     JamMsgAddSFldStr( pJam, JAMSFLD_REPLYID, GetKludgeContents( pStr ), &SubFldPos );
  397.  
  398.   else if( !strcmp( Kludge, "PID" ))
  399.     JamMsgAddSFldStr( pJam, JAMSFLD_PID, GetKludgeContents( pStr ), &SubFldPos );
  400.  
  401.   else if( !strcmp( Kludge, "SEEN-BY" ))
  402.     JamMsgAddSFldStr( pJam, JAMSFLD_SEENBY2D, GetKludgeContents( pStr ), &SubFldPos );
  403.  
  404.   else if( !strcmp( Kludge, "PATH" ))
  405.     JamMsgAddSFldStr( pJam, JAMSFLD_PATH2D, GetKludgeContents( pStr ), &SubFldPos );
  406.  
  407.   else if( !strcmp( Kludge, "FLAGS" ))
  408.     JamMsgAddSFldStr( pJam, JAMSFLD_FLAGS, GetKludgeContents( pStr ), &SubFldPos );
  409.  
  410.   else if( !strcmp( Kludge, "TZUTC" ))
  411.     JamMsgAddSFldStr( pJam, JAMSFLD_TZUTCINFO, GetKludgeContents( pStr ), &SubFldPos );
  412.  
  413.   else if( !strcmp( Kludge, "INTL" ))
  414.     {
  415.     /* Do nothing, INTL kludge shouldn't exist in HMB messagebase */
  416.     }
  417.  
  418.   else if( !strcmp( Kludge, "FMPT" ))
  419.     OrigAddr.Point = atoi( GetKludgeContents( pStr ));
  420.  
  421.   else if( !strcmp( Kludge, "TOPT" ))
  422.     DestAddr.Point = atoi( GetKludgeContents( pStr ));
  423.  
  424.   else
  425.     JamMsgAddSFldStr( pJam, JAMSFLD_FTSKLUDGE, pStr, &SubFldPos );
  426.  
  427.   return( 0 );
  428. }
  429.  
  430.  
  431. /* ---------------------------------------------------------------------- *
  432.  *
  433.  *  GetKludgeContents
  434.  *
  435.  * ---------------------------------------------------------------------- */
  436.  
  437. CHAR8 * GetKludgeContents( CHAR8 * pStr )
  438. {
  439.   CHAR8   * p;
  440.  
  441.   for( p = pStr; *p != ':' && *p != ' '; p++ )
  442.     ;
  443.   for( ; *p == ':' || *p == ' '; p++ )
  444.     ;
  445.  
  446.   return( p );
  447. }
  448.  
  449.  
  450. /* end of file "hmb2jam.c" */
  451.